package patrol.mobile.com.mobilepatrol.activity.culvert;

import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dl7.recycler.adapter.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.bingoogolapple.bgabanner.BGABanner;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.PhotoSetActivity;
import patrol.mobile.com.mobilepatrol.adapter.BridegMesAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.Handong;
import patrol.mobile.com.mobilepatrol.bean.Img;
import patrol.mobile.com.mobilepatrol.bean.Pro;
import patrol.mobile.com.mobilepatrol.manager.HManager;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.persenter.ImgPer;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.view.ImgView;

/**
 * Created by apple on 17/9/28.
 */

public class CulvertDetailActivity extends BaseActivity implements ImgView,BGABanner.Adapter<ImageView, String>, BGABanner.Delegate<ImageView, String>{
    @BindView(R.id.regular)
    Button bt_regular;

    @BindView(R.id.offen)
    Button bt_offen;

    @BindView(R.id.sign)
    Button bt_sign;

    List<Object> vlaues = new ArrayList<>();
    List<String> keys = new ArrayList<>();
    List<Pro> pros = new ArrayList<>();

    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    private BaseQuickAdapter mAdapter;

    private Handong handong;
    private String hdcode;
    @BindView(R.id.banner)
     BGABanner mBanner;
    private ArrayList<String> imglist;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_bridge_detail;
    }

    @Override
    public void setListener() {
        bt_regular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CulvertDetailActivity.this,CulvertRegularCheckListActivity.class);
                intent.putExtra("hdcode",hdcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(CulvertDetailActivity.this,false);
            }
        });
        bt_offen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CulvertDetailActivity.this,CulvertOffenCheckListActivity.class);
                intent.putExtra("hdcode",hdcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(CulvertDetailActivity.this,false);
            }
        });
        bt_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CulvertDetailActivity.this,CulvertSignActivity.class);
                intent.putExtra("hdcode",hdcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(CulvertDetailActivity.this,false);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
//


        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        boolean b = getIntent().getBooleanExtra("list",false);
        if (b){
            bt_sign.setVisibility(View.GONE);
        }
        mAdapter = new BridegMesAdapter(this,pros);
        recyclerView.setAdapter(mAdapter);
        showLoading();
        pros = getData();
        hideLoadingPanel();
        mAdapter.updateItems(pros);




    }

    @Override
    public void initView() {
        super.initView();
        mBanner.setAdapter(this);
        mBanner.setDelegate(this);
    }

    private List<Pro> getData() {
        hdcode = getIntent().getStringExtra("hdcode");

//        请求图片
        Map<String,String> map = new HashMap<>();
        map.put("hdcode",hdcode);
        map.put("userName", UserManager.getUser((String) SPUtils.get(Constants.current_id, "")).username);
        map.put("passWord", UserManager.getUser((String) SPUtils.get(Constants.current_id, "")).pw);
        ImgPer imgPer = new ImgPer(CulvertDetailActivity.this);
        imgPer.getBridgeImg(Urls.queryHdtp,map);


        handong = HManager.getHMes(hdcode);
        vlaues.add(handong.roadcode);
        vlaues.add(handong.roadname);
        vlaues.add(handong.pos);
        vlaues.add(handong.hdlx);
        vlaues.add(handong.jszk);
        vlaues.add(handong.ks);
        vlaues.add(handong.kj);
        vlaues.add(handong.jk);
        vlaues.add(handong.hdqc);
        vlaues.add(handong.jkxs);
        vlaues.add(handong.ckxs);
        vlaues.add(handong.gydwmc);
        Resources res =getResources();
        String[] culverts=res.getStringArray(R.array.culvert);
        for (int i=0;i<culverts.length;i++){
            Pro pro = new Pro(culverts[i],vlaues.get(i));
            pros.add(pro);
        }
        return pros;
    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(CulvertDetailActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("涵洞信息");
    }

    @Override
    public void onImgSuccess(List<Img> imgs) {
        if (imgs!=null&&imgs.size()>0){
             imglist = new ArrayList<>();
            for (int i=0;i<imgs.size();i++){
                imglist.add(imgs.get(i).picUrl);
            }
            mBanner.setData(imglist, null);
        }
    }

    @Override
    public void onImgError() {
        showToast("图片获取失败");
    }

    @Override
    public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
        Glide.with(this)
                .load(model)
                .placeholder(R.mipmap.ic_nav_picture)
                .error(R.mipmap.ic_nav_picture)
                .dontAnimate()
                .centerCrop()
                .into(itemView);
    }

    @Override
    public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
        Intent intent = new Intent(CulvertDetailActivity.this,PhotoSetActivity.class);
        intent.putExtra("imgs",imglist);
        startActivity(intent);
        CommonUtil.openNewActivityAnim(CulvertDetailActivity.this,false);
    }
}
